package com.huazai.apply.manager.web.action;

import com.huazai.apply.manager.bean.ApplyMessage;
import com.huazai.apply.manager.bean.User;
import com.huazai.apply.manager.utils.DbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author EZTTU
 */
@WebServlet(name = "Select", value = {"/admin", "/user"})
public class UserSelectServlet extends HttpServlet {
    private static final String SERVICE_ADMIN = "/admin";
    private static final String SERVICE_USER = "/user";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();

        String servletPath = request.getServletPath();
        User userMessage = (User) session.getAttribute("UserMessage");
        if (userMessage != null) {

            if (servletPath.equals(SERVICE_ADMIN)) {

                if (userMessage.getUserField().equals("admin")) {
                    doAdmin(request, response);
                } else {
                    response.sendRedirect(request.getContextPath());
                }
            } else if (servletPath.equals(SERVICE_USER)) {

                if (userMessage.getUserField().equals("user")) {
                    doOrdinary(request, response);
                } else {
                    response.sendRedirect(request.getContextPath());
                }
            }
        } else {
            response.sendRedirect(request.getContextPath());
        }
    }


    private void doAdmin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;

        List<ApplyMessage> messageList = new ArrayList<>();

        try {
            conn = DbUtil.getConnection();
            String sql = "select * from submitinfo order by Hsubmit desc;";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                String judgedUserid = rs.getString("Huserid");
                String result = rs.getString("Hresult");
                Date date = rs.getDate("Hsubmit");
                long applyId = rs.getLong("applyID");
                int time = rs.getInt("Htime");

                ApplyMessage message = new ApplyMessage(applyId, date, result, time);
                message.setUserId(judgedUserid);
                messageList.add(message);
            }


            request.setAttribute("MessageList", messageList);
            request.getRequestDispatcher("admin.jsp").forward(request, response);

        } catch (SQLException | ServletException e) {
            e.printStackTrace();
        } finally {
            DbUtil.close(conn, ps, rs);
        }
    }

    private void doOrdinary(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;

        HttpSession session = request.getSession();
        User userMessage = (User) session.getAttribute("UserMessage");
        List<ApplyMessage> messageList = new ArrayList<>();

        try {
            conn = DbUtil.getConnection();
            String sql = "select * from submitinfo where Huserid = ? order by Hsubmit desc";
            ps = conn.prepareStatement(sql);
            ps.setString(1, userMessage.getUserId());
            rs = ps.executeQuery();

            while (rs.next()) {
                Date submitTime = rs.getDate("Hsubmit");
                long applyId = rs.getLong("applyID");
                String result = rs.getString("Hresult");
                int time = rs.getInt("Htime");

                Time hsubmit = rs.getTime("Hsubmit");
                Timestamp timestamp = rs.getTimestamp("Hsubmit");
                System.out.println(timestamp);
                System.out.println(hsubmit);
                System.out.println(submitTime);

                ApplyMessage applyMessage = new ApplyMessage(applyId, submitTime, result, time);
                messageList.add(applyMessage);
            }

            request.setAttribute("MessageList", messageList);
            request.getRequestDispatcher("ordinary.jsp").forward(request, response);

        } catch (SQLException | ServletException e) {
            e.printStackTrace();
        } finally {
            DbUtil.close(conn, ps, rs);
        }

    }
}
